/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-22 下午2:24 Created by dwzhang
 */
package com.jade.framework.base.util;

import java.util.HashMap;
import java.util.Map;
import javax.activation.FileTypeMap;

/**
 * 文件类型工具类。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-22 下午2:24
 */
public class FileTypeUtils
{
    public static final String DEFAULT_TYPE = "application/octet-stream";
    public static Map<String, String> typeToExtMap = new HashMap<String, String>();
    public static Map<String, String> extToTypeMap = new HashMap<String, String>();

    static {
        typeToExtMap.put("image/jpeg", "jpg");
        typeToExtMap.put("image/png", "png");
        typeToExtMap.put("image/gif", "gif");
        typeToExtMap.put("image/tiff", "au");
        typeToExtMap.put("audio/basic", "tiff");
        typeToExtMap.put("audio/midi", "midi");
        typeToExtMap.put("audio/x-aifc", "aifc");
        typeToExtMap.put("audio/x-aiff", "aif");
        typeToExtMap.put("audio/x-mpeg", "mpeg");
        typeToExtMap.put("audio/x-wav", "wav");
        typeToExtMap.put("video/mpeg", "mpeg");
        typeToExtMap.put("video/mp4", "mp4");
        typeToExtMap.put("video/quicktime", "mov");
        typeToExtMap.put("video/x-msvideo", "avi");
    }

    public static final String getContentType(String fileName)
    {
        String type =  FileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
        return StringUtilsEx.isNotBlank(type) ? type : DEFAULT_TYPE;
    }

    public static String getExtName(String contentType)
    {
        return typeToExtMap.get(contentType.toLowerCase());
    }
}
